package com.ph.shopping.facade.member.service.dbservice.facade.impl;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ph.shopping.common.core.customenum.MemberEnum;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.exception.BizException;
import com.ph.shopping.common.core.other.certificates.IdAuthService;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.customfactory.CustomBeanImpl;
import com.ph.shopping.facade.customfactory.CustomBeanRule;
import com.ph.shopping.facade.customfactory.facade.ICustomBeanFactory;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.mapper.ICretificateAuthMapper;
import com.ph.shopping.facade.mapper.MemberMapper;
import com.ph.shopping.facade.member.dto.CertificatesAuthDto;
import com.ph.shopping.facade.member.entity.CertificatesAuth;
import com.ph.shopping.facade.member.entity.Member;
import com.ph.shopping.facade.member.service.dbservice.facade.ICretificateAuthDbService;

@Service
public class CretificateAuthDbService implements ICretificateAuthDbService{

	private static final Logger log = LoggerFactory.getLogger(CretificateAuthDbService.class);
	
	@Autowired
	private IdAuthService idAuthService;

	@Autowired
	private ICustomBeanFactory<CustomBeanRule> customBeanFactory;
	
	@Autowired
	private ICretificateAuthMapper authMapper;
	
	@Autowired
	private MemberMapper memeberMapper;
	
	@Override
	@Transactional
	public Result idCretificateAuth(CertificatesAuthDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		if(isCretificateAuth(dto.getIdCode())){
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			result.setMessage("身份证已认证");
			return result;
		}
		boolean flag = false;
		try {
			flag = idAuthService.idCertificatesAuth(dto.getName(), dto.getIdCode());
		} catch (Exception e) {
			throw new BizException("调用认证接口 证件认证失败", e);
		}
		if (flag) {
			String[] fields = { "certificatesName:name", "certificatesCode:idCode"};
			Map<String, Object> targetValue = ContainerUtil.map();
			targetValue.put("createTime", new Date());
			targetValue.put("certificatesType", MemberEnum.IDENTITY_CERTIFICATES.getCode());
			targetValue.put("status", MemberEnum.ID_AUTH_SUCCESS.getCode());
			targetValue.put("userId", dto.getUserId().toString());
			CertificatesAuth ca = getCertificatesAuth(CertificatesAuth.class, dto, fields, targetValue);
			if (ca != null) {
				authMapper.insert(ca);
				Member member = new Member();
				member.setId(dto.getUserId());
				member.setCertification(MemberEnum.CERTIFICATION.getCode());
				member.setIdCard(dto.getIdCode());
				memeberMapper.updateMember(member);//修改会员表认证状态
				result = DubboResult.getResultBySuccess(result);
			}
		}
		return result;
	}
	/**
	 * 
	 * @Title: isCretificateAuth   
	 * @Description: 判断是否已认证  
	 * @param: @param idCode
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	private boolean isCretificateAuth(String idCode){
		CertificatesAuth certificatesAuthauth = authMapper.selectCretificateAuthInfo(idCode);
		return certificatesAuthauth != null 
				&& MemberEnum.ID_AUTH_SUCCESS.getCode().equals(certificatesAuthauth .getStatus());
	}
	/**
	 * 
	* @Title: getCertificatesAuth  
	* @Description: TODO(这里用一句话描述这个方法的作用)  
	* @param @param clas
	* @param @param obj
	* @param @return    参数  
	* @return T    返回类型  
	* @throws
	 */
	private <T> T getCertificatesAuth(Class<T> clas, Object obj, String[] fields,Map<String, Object> targetValue) {
		T t = null;
		try {
			if (obj != null) {
				CustomBeanRule customBean = new CustomBeanRule(
						CustomBeanImpl.getKeyValues(fields), obj,targetValue);
				
				t = customBeanFactory.generateAppointBean(customBean, clas);
			}
		} catch (Exception e) {
			log.error("obj to t error", e);
		}
		return t;
	}

	@Override
	public Result queryCretificateAuthInfo(String idCode) {
		Result result = DubboResult.getDefResultByImpl();
		CertificatesAuth certificatesAuthauth = authMapper.selectCretificateAuthInfo(idCode);
		if(certificatesAuthauth != null){
			result.setData(certificatesAuthauth);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}
}
